﻿using OASystem.BLL.IService;
using OASystem.Model;
using OASystem.Models;
using OASystem.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace OASystem.Controllers
{
    public class MyrecodeController : Controller
    {
        #region 注入
        private IMyrecodeService _myrecodeService;
        public MyrecodeController(IMyrecodeService myrecodeService)
        {
            _myrecodeService = myrecodeService;
        }
        #endregion

        /// <summary>
        /// 显示列表
        /// </summary>
        /// <param name="page">页数</param>
        /// <param name="limit">记录数量</param>
        /// <param name="Publisher">发布人</param>
        /// <param name="TaskTiltle">任务标题</param>
        /// <param name="CompletionTime">发布时间</param>
        /// <returns></returns>
        public JsonResult Display(int page, int limit, string Publisher, string TaskTiltle, string CompletionTime)
        {
            Userinfo userinfo = AdminContext.context.adminUser;
            ResponseResult result = new ResponseResult();
            result = _myrecodeService.Display(userinfo.UserId, Publisher, TaskTiltle, CompletionTime);
            PageListResult<Taskquery> a = new PageListResult<Taskquery>();
            List<Taskquery> admins = null;
            if (result != null)
            { admins = result.data; }
            if (admins == null)
            { a.count = 0; a.data = null; }
            else
            { a.count = admins.Count; a.data = admins.Skip((page - 1) * limit).Take(limit).ToList(); }
            a.code = 0;
            a.msg = "";
            return Json(a, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        public FileResult ToFile()
        {
            List<Taskquery> task = new List<Taskquery>();
            Userinfo userinfo = AdminContext.context.adminUser;
            ResponseResult result = new ResponseResult();
            result = _myrecodeService.Display(userinfo.UserId, null, null, null);
            task = result.data;
            var sbHtml = new StringBuilder();
            sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
            sbHtml.Append("<tr>");
            var lstTitle = new List<string> { "任务标题", "发布人", "执行人", "发布时间", "执行时间", "任务内容", "是否完成" };
            foreach (var item in lstTitle)
            {
                sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color:#DCE0E2;font-weight:bold;'height='25'>{0}</td>", item);
            }
            sbHtml.Append("</tr>");

            for (int i = 0; i < task.Count; i++)
            {
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat("<td>{0}</td>", task[i].TaskTiltle);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].Publisher);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].oneer);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].CompletionTime);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].ReleaseTime);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].TaskContent);
                sbHtml.AppendFormat("<td>{0}</td>", task[i].Status);
                sbHtml.Append("</tr>");
            }
            sbHtml.Append("</table>");

            byte[] fileContents = Encoding.Default.GetBytes(sbHtml.ToString());
            return File(fileContents, "application/ms-excel", "我的任务.xls");
        }

    }
}